package com.lixin.mail.product.service.impl;

import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lixin.common.utils.PageUtils;
import com.lixin.common.utils.Query;

import com.lixin.mail.product.dao.CategoryDao;
import com.lixin.mail.product.entity.CategoryEntity;
import com.lixin.mail.product.service.CategoryService;


@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {

    @Autowired
    private CategoryService categoryService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryEntity> page = this.page(
                new Query<CategoryEntity>().getPage(params),
                new QueryWrapper<CategoryEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 查询所有的分类及子分类，以树形结构展示出来
     * @return
     */
    @Override
    public List<Tree<Long>> setList() {
        //获取部门列表数据
        List<CategoryEntity> depts = categoryService.list();
        //配置类
        TreeNodeConfig treeNodeConfig = new TreeNodeConfig();
        treeNodeConfig.setWeightKey("sort");
        treeNodeConfig.setIdKey("catId");
        treeNodeConfig.setParentIdKey("parentCid");
        //构建树形
        List<Tree<Long>> trees = TreeUtil.build(depts, 0L, treeNodeConfig, ((object, treeNode) -> {
            treeNode.setId(object.getCatId());//id
            treeNode.setParentId(object.getParentCid());//父id
            treeNode.putExtra("name", object.getName());
            treeNode.putExtra("catLevel", object.getCatLevel());
            treeNode.putExtra("showStatus", object.getShowStatus());
            treeNode.putExtra("sort", object.getSort());
            treeNode.putExtra("icon", object.getIcon());
            treeNode.putExtra("productUnit", object.getProductUnit());
            treeNode.putExtra("productCount", object.getProductCount());
        }));
        return trees;

    }

    /**
     * 查询所有的分类及子分类，以树形结构展示出来
     */
    @Override
    public List<CategoryEntity> setList2() {
        List<CategoryEntity> list = categoryService.list();
        List<CategoryEntity> retList = new ArrayList<>();
        Map<Long, CategoryEntity> id_entity = list.stream().collect(Collectors.toMap(CategoryEntity::getCatId, e -> e));
        for (CategoryEntity entity : list) {
            if (entity.getFlag()){
                if (entity.getParentCid() != 0){
                    // 自己是子级 递归去找父级
                    CategoryEntity parentEntity = id_entity.get(entity.getParentCid());
                    List<CategoryEntity> childrenList = parentEntity.getChildrenList();
                    childrenList.add(entity);
                    childrenList = childrenList.stream().sorted(Comparator.comparing(CategoryEntity::getSort)).collect(Collectors.toList());
                    parentEntity.setChildrenList(childrenList);
                    // 自己的父级也有父级 找爷爷
                    if (parentEntity.getParentCid() != 0 && parentEntity.getFlag()) {
                        findParent(retList,id_entity,parentEntity);
                    }else {
                        entity.setFlag(false);
                    }
                }else {
                    entity.setFlag(false);
                    retList = retList.stream().sorted(Comparator.comparing(CategoryEntity::getSort)).collect(Collectors.toList());
                    retList.add(entity);
                }
            }
        }
        return retList;
    }

    private void findParent( List<CategoryEntity> retList ,  Map<Long, CategoryEntity> id_entity,CategoryEntity entity) {
        // 自己是子级 递归去找父级
        CategoryEntity parentEntity = id_entity.get(entity.getParentCid());
        List<CategoryEntity> childrenList = parentEntity.getChildrenList();
        childrenList.add(entity);
        childrenList = childrenList.stream().sorted(Comparator.comparing(CategoryEntity::getSort)).collect(Collectors.toList());
        parentEntity.setChildrenList(childrenList);
        entity.setFlag(false);
        if (parentEntity.getParentCid() != 0 && parentEntity.getFlag()) {
            findParent(retList,id_entity,parentEntity);
        }
    }

}